package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */

@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;



    @GetMapping("/depts")
    public Result list() {
        log.info("查询所有部门列表");
        List<Dept> list = deptService.list();

        return Result.success(list);
    }



    @DeleteMapping("/depts/{id}")
    public  Result delete(@PathVariable Integer id){
        log.info("根据id删除部门");
        deptService.deleteById(id);
        return  Result.success();
    }

    @PostMapping("/depts")
    public  Result add(@RequestBody Dept dept){
        log.info("添加部门 {}" + dept);
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping("/depts/{id}")
    public  Result getById(@PathVariable  Integer id){
        log.info("查询当前id的部门 {}"+id);
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("修改部门 {}"+ dept);
        deptService.update(dept);
        return  Result.success();
    }


}
